#include "EventPoller.h"
#include <cassert>
#include <algorithm>

void EventPoller::AddHandler(EventHandler* eh)
{
	m_handlers.push_back(eh);
}
void EventPoller::RemoveHandler(EventHandler* eh)
{
	Handlers::iterator it = std::find(m_handlers.begin(), m_handlers.end(), eh);
	assert(it != m_handlers.end());
	{
		m_handlers.erase(it);
	}
}

void EventPoller::Update()
{
	SDL_Event event;
	while (SDL_PollEvent(&event))
	{
		// Copy the vector, and iterate over the copy, so as to not remove an element being iterated over
		Handlers h = m_handlers;
		for (Handlers::iterator it = h.begin();
			it != h.end();
			++it)
		{
			(*it)->HandleEvent(event); //current element
		}
	}
}